/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.autocheck.impl;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.service.cronjob.autocheck.IAutoAuditCheckerChain;
import com.ceair.flight.refund.service.cronjob.autocheck.IAutoChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 *
 * @author youguilin
 */
@Service
public class DefaultAutoAuditCheckerChain implements IAutoAuditCheckerChain {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultAutoAuditCheckerChain.class);
    @Autowired
    private List<IAutoChecker> autoChecker;

    private IAutoChecker startChecker;

    @PostConstruct
    void initial() {
        LOG.debug("Initializing auto audit checker chain");
        Assert.notNull(autoChecker, "Auto checker list is null");
        Assert.isTrue(!autoChecker.isEmpty(), "Auto checker list is empty");
        for (int i = 0; i < autoChecker.size() - 1; i++) {
            autoChecker.get(i).setNextChecker(autoChecker.get(i + 1));
        }
        startChecker = autoChecker.get(0);
    }

    @Override
    public boolean isAutoAuditable(RefundApplyDto refundApply) {
        return startChecker.autoCheck(refundApply);
    }

}
